
#include "flash.h"

#include <stdio.h>
#include <string.h>
#include "main.h"

extern uint8_t Wireless_Mode; 
extern eDeviceMode gDeviceRunMode;

extern uint16_t	Fire_Hydrant_Range;
extern uint16_t	Water_Pressure_Range;
extern uint16_t	Water_Level_Range;

uint32_t addr = 0x0800e000; 

static FLASH_EraseInitTypeDef EraseInitStruct = {
	.TypeErase = FLASH_TYPEERASE_PAGES,
	.PageAddress = 0x0800e000,
	.NbPages = 1
};

uint8_t Read_Config(void)
{
	uint32_t readFlashData;
	uint8_t Config_Data[20];
	uint8_t i,j;
	uint16_t Read_Range;
	
	HAL_FLASH_Unlock();
	for(i=0;i<5;i++)
	{
		readFlashData = *(__IO uint32_t*)(addr+(i<<2));
		Config_Data[4*i+3] = readFlashData>>24;
		Config_Data[4*i+2] = readFlashData>>16;
		Config_Data[4*i+1] = readFlashData>>8;
		Config_Data[4*i+0] = readFlashData;
	}
	SysPrintf("read config data:");
	for(i=0;i<20;i++)
	{
		SysPrintf("%c ",Config_Data[i]);
	}
	SysPrintf("\r\n");
	
	HAL_FLASH_Lock();
	if( (strstr(Config_Data,"setdev=")!=NULL) && (strstr(Config_Data,"AA")!=NULL) ) 
	{
		if( (Config_Data[0]=='s') && (Config_Data[6]=='=') && (Config_Data[8]==',') && (Config_Data[10]==',') && (Config_Data[15]=='A') && (Config_Data[16]=='A'))
		{
			if(Config_Data[7]=='0')
			{			
				Wireless_Mode = Nb_Mode;
				SysPrintf("commu-type Nb_Mode\r\n");
			}
			else if(Config_Data[7]=='1')
			{
				Wireless_Mode = Lora_Mode;
				SysPrintf("commu-type Lora_Mode\r\n");
			}
			else if(Config_Data[7]=='2')
			{
				Wireless_Mode = LoRaWAN_Mode;
				SysPrintf("commu-type LoRaWAN_Mode\r\n");
			}
			else
			{
				SysPrintf("commu-type err\r\n");
				return 0;
			}
		}
		
		if((Config_Data[9]>='1') && (Config_Data[9]<='5'))
		{
			gDeviceRunMode = Config_Data[9] - '0';
		}
		else
		{
			SysPrintf("device-type err\r\n");
			return 0;
		}
		
		for(j=11;j<15;j++)
		{
			if((Config_Data[j]>='0') && (Config_Data[j]<='9'))
				;
			else
			{
				SysPrintf("device-range err and use default config-info\r\n");
				return 0;
			}
		}
		
		Read_Range = 1000*(Config_Data[11]-'0') + 100*(Config_Data[12]-'0') + 10*(Config_Data[13]-'0') + 1*(Config_Data[14]-'0');
		
		if(gDeviceRunMode==Fire_Hydrant_Mode) 
			Fire_Hydrant_Range = Read_Range;
		else if(gDeviceRunMode==Water_Pressure_Mode) 
			Water_Pressure_Range = Read_Range;
		else if(gDeviceRunMode==Water_Level_Mode) 
			Water_Level_Range = Read_Range;
		else
		;
		return 1;
		
	}
	else
	{
		SysPrintf("read config-info err\r\n");
		return 0;
	}
}
	


uint8_t Write_Config(uint8_t *buf,uint8_t len)
{
	uint32_t PageError = 0;
	uint32_t writeFlashData[5];
	uint8_t i = 0;
	uint8_t Config_Data[20];
	SysPrintf("write config data to flash begin\r\n");
	if(len<20)
	{
		for(i=0;i<len;i++)
			Config_Data[i] = buf[i];
		for(i=len;i<20;i++)
			Config_Data[i] = 0;
	}
	else
	{
		SysPrintf("length err\r\n");
		return 0;
	}
		
	if(Check_Config_Data(buf,len)==1)
	{
		HAL_FLASH_Unlock();
		if (HAL_FLASHEx_Erase(&EraseInitStruct,&PageError) == HAL_OK)
		{
			SysPrintf("Erase page success\r\n");
		}
		for(i=0;i<sizeof(Config_Data);i+=4)
	{
		HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD,addr+i,*(uint32_t*)(Config_Data+i));
	}
		









		HAL_FLASH_Lock();
		SysPrintf("write config data to flash end\r\n");
		return 1;
	}
	else
	{
		SysPrintf("format err not write flash\r\n");
		return 0;
	}
}


uint8_t Check_Config_Data(uint8_t *buf,uint8_t len)
{
	uint8_t j = 0;
	SysPrintf("check recv cmd format\r\n");
	if(len==17)
	{
		if( (strstr(buf,"setdev=")!=NULL) && (strstr(buf,"AA")!=NULL) ) 
		{
			if( (buf[0]=='s') && (buf[6]=='=') && (buf[8]==',') && (buf[10]==',') && (buf[15]=='A') && (buf[16]=='A'))
			{
				if((buf[7]=='0') || (buf[7]=='1') || (buf[7]=='2') )
				{
					SysPrintf("commu-type format ok\r\n");;
				}
				else
				{
					SysPrintf("commu-type format err\r\n");
					return 0;
				}
			
				if((buf[9]>='1') && (buf[9]<='5'))
				{
					SysPrintf("device-type format ok\r\n");
				}
				else
				{
					SysPrintf("device-type format err\r\n");
					return 0;
				}
				
				for(j=11;j<15;j++)
				{
					if((buf[j]>='0') && (buf[j]<='9'))
					{
						;
					}

					else
					{
						SysPrintf("device-range format err\r\n");
						return 0;
					}
				}
				SysPrintf("device-range format ok\r\n");
				return 1;
			
			}
		}
		else
		{
			SysPrintf("format err\r\n");
			return 0;
		}
	}
	else
	{
		SysPrintf("length err\r\n");
		return 0;
	}
}	


